/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * \file
 * \brief Implementation of USB Panic Button "driver"
 * \author Matthieu Myrsky <matthieu.myrsky@tkk.fi>
 */
#include "USBPanicButton.hpp"

CUSBPanicButton::CUSBPanicButton()
  : CThread(1),
    iRunning(false),
    iUsb(),
    iCallback(NULL)
{

}
//*****************************************************************************

CUSBPanicButton::~CUSBPanicButton()
{

  Close();
}
//*****************************************************************************

bool CUSBPanicButton::Open()
{
  bool result = false;
  if(iCallback && !iRunning){

    struct usb_device *devs[10];
    int  numberOfDevices = iUsb.SearchForDevice(VENDORID,PRODUCTID,devs);
    dPrint(1,"numberOf devs %d",numberOfDevices);
    if(numberOfDevices > 0){
      
      if(iUsb.Open(devs[0])){
        dPrint(1,"USB Opened succesfully");
        iRunning = true;
        CThread::RunThread(0);      
        result = true;
      }else{
        dPrint(1,"Failed to open USB");
        result = false;
      }
    }else{
      dPrint(1,"No USB device found");
      result = false;

    }
  }else{
    dPrint(3,"Please set callback function before opening");
    result = false;
  }
  return result;
}
//*****************************************************************************

bool CUSBPanicButton::Close()
{
  if(iRunning){
    iRunning = false;
    iUsb.Close();
    return true;
  }else{
    dPrint(3,"Device already closed");
    return false;
  }


}
//*****************************************************************************

int CUSBPanicButton::ThreadFunction(const int aThreadNumber)
{

  while(iRunning){


    char buffer[8];
    *buffer = 0;
       
    iUsb.ControlMessage(buffer, 8,5000,
                       REQUEST_TYPE,
                       1,
                       REQUEST_VALUE,
                       0);
    if((*buffer)){   
      dPrint(1,"Pressed");
      iCallback->OnUSBPanicButtonPressed();
    }
    ownSleep_ms(30);

  }
  return 0;

}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
